package com.aoip.telemetry.telemetry.configureui.business.query.report.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.query.report.mapper.ReportStatisticsQueryMapper;
import com.aoip.telemetry.telemetry.configureui.business.query.report.service.ReportStatisticService;
import com.aoip.telemetry.telemetry.entity.common.ReportStatisticsEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/1/7 16:45
 */
@Service
public class ReportStatisticServiceImpl implements ReportStatisticService {

    @Resource
    private ReportStatisticsQueryMapper mapper;
    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Override
    public List<ReportStatisticsEntity> list(ReportStatisticsEntity entity) {
        String beginStatisticTimeCn = entity.getBeginStatisticTimeCn();
        if (StringUtils.hasText(beginStatisticTimeCn)) {
            entity.setBeginStatisticTime(LocalDate.parse(beginStatisticTimeCn, DTF));
        }
        String endStatisticTimeCn = entity.getEndStatisticTimeCn();
        if (StringUtils.hasText(endStatisticTimeCn)) {
            entity.setEndStatisticTime(LocalDate.parse(endStatisticTimeCn, DTF));
        }
        List<ReportStatisticsEntity> list = mapper.list(entity);
        list.forEach(item -> {
            if (StringUtils.hasText(beginStatisticTimeCn)) {
                item.setBeginStatisticTimeCn(beginStatisticTimeCn);
            }
            if (StringUtils.hasText(endStatisticTimeCn)) {
                item.setEndStatisticTimeCn(endStatisticTimeCn);
            }
        });
        return list;
    }
}
